//
//  LiuHomeTableViewCell.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/30.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit
import SnapKit
class LiuHomeTableViewCell: UITableViewCell {

    var toolBarTopConstraint:Constraint?
    
    //viewmodel
    var viewModel:LiustatuseViewModel? {
        didSet {
            originalView.statuse = viewModel
            toolBar.statuse = viewModel
            
            //根据数据判断约束
            //  1. 把之前的toolbar的顶部约束卸载掉,重写根据数据创建对应的顶部约束, 要保证toolbar上面只有一条顶部约束
            toolBarTopConstraint?.deactivate()
            
            //  判断是否有转发微博数据
            if viewModel?.homeStatuse?.retweeted_status != nil {
                //  如果想要更新某个约束,如果约束不存在那么会崩溃
                toolBar.snp.makeConstraints({ (make) in
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                //  有转发微博
                retweetView.statuse = viewModel
                //  有转发显示转发视图
                retweetView.isHidden = false
            } else {
                //  没有转发微博
                //  隐藏转发微博视图
                retweetView.isHidden = true
                //  如果想要更新某个约束,如果约束不存在那么会崩溃
                toolBar.snp.makeConstraints({ (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp.bottom).offset(1.0 / UIScreen.main.scale).constraint
                })
            }

        }
    }
    
    //懒加载控件
    lazy var  originalView:LiuOriginalView = LiuOriginalView()
    lazy var  retweetView:LiuRetweetView = LiuRetweetView()
    lazy var  toolBar:LiuToolBarView = LiuToolBarView()
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    fileprivate func setupUI() {
        contentView.backgroundColor = UIColor.groupTableViewBackground
        
        //关闭cell的点击效果
        selectionStyle = .none
        
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        //自动布局
        originalView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(contentView)
        }
        
        retweetView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.left.right.equalToSuperview()
        }
        
        toolBar.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(30)
            toolBarTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.bottom.equalToSuperview().offset(-homeMargin)
        }
    }
}
